import { resolve } from "@tauri-apps/api/path";
import axios from "axios";
import axiosTauriApiAdapter from "axios-tauri-api-adapter";
//import axiosTauriHttpAdapter from "axios-tauri-http-adapter";
import { reject } from "lodash";

const baseURL =import.meta.env.VITE_APP_BASE_API;
const env =import.meta.env.VITE_APP_ENV
const env1= JSON.stringify(import.meta.env)
console.log(`${env} ${process.env.NODE_ENV} ${baseURL}`)
console.log(env1)

axios.defaults.headers["Content-Type"] = "application/json;charset=utf-8";
// 创建axios实例
const client = axios.create({
  adapter: axiosTauriApiAdapter,
  //adapter: axiosTauriHttpAdapter(),
  // axios中请求配置有baseURL选项，表示请求URL公共部分
  baseURL: import.meta.env.VITE_APP_BASE_API,
  // 超时import { request } from '@/utils/request';

  timeout: 10000,
  headers: {
    Accept: "application/json, text/plain, */*",
    "Content-Type": "application/json",
  },
});
// let BASE_API_URL = "http://192.168.100.223:86";

client.interceptors.request.use(
  (config) => {
    // config.url = `${config.baseURL}${config.url}`;

    console.log(config);
    return config;
  },
  (error) => {
    // alert(err)
    return Promise.reject(error);
  }
);

client.interceptors.response.use(
  (response) => {
    //2xx
    return response;
  },
  (error) => {
    alert(err)
    
    const { response } = error;
    if (response) {
      return Promise.reject(response.data);
    } else {
      //message warnin()
      let msg = "网络连接异常,请稍后再试!";
      return Promise.reject(error);
    }
  }
);

export function request(data) {
  alert(JSON.stringify(data))
  return new Promise((resolve, reject) => {
    const promise = client(data);
    promise
      .then((resp) => {
        // alert(resp)
        resolve(resp.data);
      })
      .catch((err) => {
        // alert(resp)
        reject(err);
      });
  });
}

/**
 * GET
 * @param {string} url -
 * @param {Object} [params] -  querystring
 * @returns {Promise} -  Promise
 */
export function get(url, params) {
  return request({ method: "GET", url: url, params: params });
}
/**
 * POST
 * @param {string} url -
 * @param {Object} data -
 * @returns {Promise} -  Promise
 */
export function post(url, data) {
  return request({ method: "POST", url: url, data: data });
}
/**
 * PUT 请求
 * @param {string} url - 请求url
 * @param {Object} data - 请求参数
 * @returns {Promise} - Promise对象
 */
export function put(url, data) {
  return request({ method: "PUT", url: url, data: data });
}
/**
 * DELETE
 * @param {string} url -
 * @param {Object} data -  request body
 * @returns {Promise} -  Promise
 */
export function del(url) {
  return request({ method: "DELETE", url: url });
}

export default request;
